<html>
<head><meta charset="utf-8"><title>Taking ownership of heap allocated C string? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F.html">Taking ownership of heap allocated C string?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239219356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Taking%20ownership%20of%20heap%20allocated%20C%20string%3F/near/239219356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F.html#239219356">(May 18 2021 at 08:52)</a>:</h4>
<p>Hi,</p>
<p>Is there a correct way,  given a <code>*const c_char</code>, for Rust to take ownership of a string (char array) allocated on the heap by C?</p>
<p>I was about to reach for <code>CString::from_raw()</code>, but the docs warn against this kind of usage:</p>
<blockquote>
<p>This should only ever be called with a pointer that was earlier obtained by calling CString::into_raw. Other usage (e.g., trying to take ownership of a string that was allocated by foreign code) is likely to lead to undefined behavior or allocator corruption.</p>
</blockquote>
<p>As I understand it, <code>CStr::from_ptr()</code> would be a borrow, whereas I'd like Rust to take ownership and deallocate the buffer when it is dropped.</p>
<p>Can it be done? Thanks.</p>



<a name="239220574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Taking%20ownership%20of%20heap%20allocated%20C%20string%3F/near/239220574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F.html#239220574">(May 18 2021 at 09:02)</a>:</h4>
<p>as I understand it, any attempt at taking something allocated by C and deallocating it from Rust would be UB, so there is fundamentally no way to do this</p>



<a name="239220852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Taking%20ownership%20of%20heap%20allocated%20C%20string%3F/near/239220852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F.html#239220852">(May 18 2021 at 09:04)</a>:</h4>
<p>you could write a custom owning wrapper that calls the correct C function to deallocate it on drop, though</p>



<a name="241680406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Taking%20ownership%20of%20heap%20allocated%20C%20string%3F/near/241680406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Demi Obenour <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F.html#241680406">(Jun 06 2021 at 08:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F/near/239220852">said</a>:</p>
<blockquote>
<p>you could write a custom owning wrapper that calls the correct C function to deallocate it on drop, though</p>
</blockquote>
<p>You can also implement <code>Deref</code> for this wrapper.</p>



<a name="241698678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Taking%20ownership%20of%20heap%20allocated%20C%20string%3F/near/241698678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Taking.20ownership.20of.20heap.20allocated.20C.20string.3F.html#241698678">(Jun 06 2021 at 16:16)</a>:</h4>
<p>Also, if you know that the string was allocated by the same allocator that Rust is using, it's perfectly safe to deallocate it in Rust.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>